package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootSociaty;
import org.footballmanager.pandafootball.system.form.impl.FootSociatySearchForm;
import org.footballmanager.pandafootball.system.service.FootSociatyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@SessionAttributes("footSociatySearchForm")
@RequestMapping("/hy/footSociaty")
public class HySociatyController extends BaseController{
    /* 实体名称 */
    private static final String ENTITYNAME = "footSociaty";
	private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.HY, ENTITYNAME);
	private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.HY, ENTITYNAME);
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);
	private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.HY, ENTITYNAME);

	@Autowired
	private FootSociatyService footSociatyService;

    /**
     * 初始化
     * @param map 参数
     * @param footSociatySearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootSociatySearchForm footSociatySearchForm){
        FootSociatySearchForm newFootSociatySearchForm = new FootSociatySearchForm();
        if(footSociatySearchForm != null){
            newFootSociatySearchForm = footSociatySearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootSociatySearchForm);
    }

    /**
     * 战队管理列表
     * @param map 参数
     * @param footSociatySearchForm 条件
     * @return 页面
     */
	@RequestMapping(value="/search", method= RequestMethod.GET)
	public String search(Model map, @ModelAttribute FootSociatySearchForm footSociatySearchForm) {
        footSociatyService.search(ENTITYNAME, map, footSociatySearchForm);
        return LISTPAGE;
	}

    /**
     * 战队管理列表
     * @param map 参数
     * @param footSociatySearchForm 条件
     * @return 页面
     */
    @RequestMapping(value="/join", method= RequestMethod.GET)
    public String join(Model map, @ModelAttribute FootSociatySearchForm footSociatySearchForm) {
        footSociatyService.search(ENTITYNAME, map, footSociatySearchForm);
        return "hy/footSociaty/Join";
    }

    /**
     * 打开添加战队管理页面
     * @param map 参数
     * @return 页面
     */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addAddPage(Model map) {
		map.addAttribute(ENTITYNAME, new FootSociaty());
		return ADDPAGE;
	}

    /**
     * 保存战队管理记录
     * @param footSociaty 战队管理实体类
     * @param bindingResult 校验
     * @param request 请求
     * @return 页面
     */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String save(@Valid @ModelAttribute FootSociaty footSociaty, BindingResult bindingResult,
                       HttpServletRequest request, @RequestParam MultipartFile img, SessionStatus s) {
		if (!bindingResult.hasErrors()) {
			footSociatyService .save(footSociaty, img, request);
			s.setComplete();
			return REDIRECT;
		} else 
			return ADDPAGE;
	}

    /**
     * 打开编辑战队管理页面
     * @param map 参数
     * @param id 战队主键
     * @return 页面
     */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String openEditPage(Model map, HttpServletRequest request) {
        Long userId = (Long)request.getSession().getAttribute(Constants.USERID);
        map.addAttribute(ENTITYNAME, footSociatyService.findFootSociatyByUserid(userId));
		return EDITPAGE;
	}

    /**
     * 更新战队管理记录
     * @param footSociaty 战队管理实体类
     * @param bindingResult 校验
     * @param request 请求
     * @return 页面
     */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute FootSociaty footSociaty, BindingResult bindingResult,
						 HttpServletRequest request, @RequestParam MultipartFile img, SessionStatus s){
		if (!bindingResult.hasErrors()) {
			footSociatyService .update(footSociaty, img, request);
			s.setComplete();
			return REDIRECT;
		} else {
			return EDITPAGE;
		}
	}

    /**
     * 浏览战队管理页面
     * @param map 参数
     * @param id 主键
     * @param footSociatySearchForm 条件
     * @return 页面
     */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootSociatySearchForm footSociatySearchForm) {
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footSociatySearchForm);
		map.addAttribute(ENTITYNAME, footSociatyService.get(id));
		return VIEWPAGE;
	}

}
